import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {Router} from "@angular/router";
import {AuthService} from "../../share/services/auth.service";
import {environment} from "../../../environments/environment";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  public loginForm: FormGroup;

  constructor(private formBuilder: FormBuilder, private router: Router, private authService:AuthService) { }

  ngOnInit() {
    console.debug('LoginComponent init')
/*    this.loginForm = this.formBuilder.group({
      username: ['', [Validators.required]],
      password: ['', [Validators.required]],
    });*/
    this.loginForm = new FormGroup({
      username: new FormControl(),
      password: new FormControl(),
    });
  }
  onSubmit(formValue){
    //console.debug(formValue)
    // this.router.navigateByUrl('/pages')
    this.authService.login(formValue).subscribe(
      userInfo => {
        this.router.navigateByUrl('/pages')
        console.log(userInfo)
      },
      error => {
        console.error(error)
        // this.router.navigateByUrl('/pages')
      }
    )
  }
  setHome(){
    let url = location.href
    alert("抱歉，您所使用的浏览器无法完成此操作。\n\n您需要手动将【"+url+"】设置为首页。");
  }
  addFavorite(){
    alert("抱歉，您所使用的浏览器无法完成此操作。\n\n加入收藏失败，请手工进行添加");
  }
  help(){

  }
}

